// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import AudioManager from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import PlatformManager from "../../Platforms/PlatformManager";
import BaseUI from "../BaseUI";



const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerInfoUI extends BaseUI {

    @property(cc.Label)
    playerNameTxt: cc.Label = null;
    @property(cc.Label)
    userNameTxt: cc.Label = null;
    @property(cc.Toggle)
    bgmToggle: cc.Toggle = null;
    @property(cc.Toggle)
    effectToggle: cc.Toggle = null;
    @property([cc.Toggle])
    headList: cc.Toggle[] = [];
    @property(cc.Node)
    dialogUI: cc.Node = null;
    @property(cc.EditBox)
    nameEditBox: cc.EditBox = null;
    @property(cc.Label)
    changeNameCostTxt: cc.Label = null;

    headIds: number[] = [1001, 1002, 1003, 1004, 2001, 2002, 2003];
    protected onEnable(): void {
        var userdata = SaveManager.single.userdata;
        this.bgmToggle.isChecked = userdata.isPlayBgm;
        this.effectToggle.isChecked = userdata.isPlayeAudioEffect;


        for (let i = 0; i < this.headList.length; i++) {
            if (userdata.playerHeadId == this.headIds[i]) {
                this.headList[i].isChecked = true;
                break;
            }
        }
        if (userdata.isFirstChangeName) {
            this.changeNameCostTxt.string = "0";
        } else {
            this.changeNameCostTxt.string = "10";
        }

        this.playerNameTxt.string = userdata.playerName;
        this.userNameTxt.string = SaveManager.single.accountData.userId + "";
        GameEvent.eventNode.on(GameEvent.RENAME_EVENT, this.onRename, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(GameEvent.RENAME_EVENT, this.onRename, this);
    }
    onRename() {
        var userdata = SaveManager.single.userdata;
        this.playerNameTxt.string = userdata.playerName;
    }


    bgmToggleClick() {
        var userdata = SaveManager.single.userdata;
        userdata.isPlayBgm = this.bgmToggle.isChecked;

        SaveManager.single.saveData();
        AudioManager.single.playBgm();
    }

    effectToggleClick() {
        var userdata = SaveManager.single.userdata;
        userdata.isPlayeAudioEffect = this.effectToggle.isChecked;
        SaveManager.single.saveData();
    }

    headToggleClick(e: cc.Event, id: string) {
        var userdata = SaveManager.single.userdata;
        userdata.playerHeadId = Number(id);
        SaveManager.single.saveData();

        GameEvent.eventNode.emit(GameEvent.CHANGEHEADID_EVET);
    }

    closeDialogUI() {
        this.dialogUI.active = false;
    }
    showDialogUI() {
        this.dialogUI.active = true;

        var userdata = SaveManager.single.userdata;
        if (userdata.isFirstChangeName) {
            this.changeNameCostTxt.string = "0";
        } else {
            this.changeNameCostTxt.string = "10";
        }
    }

    async changeNameClick() {
        if (SaveManager.single.userdata.isFirstChangeName == false && !SaveManager.single.isEnoughDiamond(10)) {
            UIManager.single.showTips("钻石不足。");
            return;
        }

        if (this.nameEditBox.string.length == 0) {
            UIManager.single.showTips("请输入昵称。");
            return;
        }
        let result = await PlatformManager.single.basePlatform.sdk.sensitiveWordFilter(this.nameEditBox.string);
        if (result.code == 200) {
            if (result.data.isSensitive) {
                UIManager.single.showTips("修改昵称含敏感词，请重新输入。");
            } else {
                SaveManager.single.changeName();
                var usedata = SaveManager.single.userdata;
                usedata.playerName = result.data.filterText;
                usedata.diamond -= 10;
                SaveManager.single.saveData();
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
                GameEvent.eventNode.emit(GameEvent.RENAME_EVENT);
                this.closeDialogUI();
                UIManager.single.showTips("改名成功。");
            }
        } else {
            //UIManager.single.showTips(result.message);
            UIManager.single.showTips("修改昵称含敏感词，请重新输入。");
        }

    }


}
